//
//  WMEUserBack.m
//  everyDay_duiTang
//
//  Created by 恩叔 on 16/1/23.
//  Copyright © 2016年 enshu. All rights reserved.
//

#import "WMEUserBack.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "UIView+hhhh.h"

#define SIZE 20

#pragma maek - 用户界面的头部视图

@interface WMEUserBack ()<UIScrollViewDelegate>
@property (nonatomic, retain)UIView *head;
@property (nonatomic, retain)UIImageView *backImg;
@property (nonatomic, retain)UIImageView *userImg;
@property (nonatomic, retain)UIImageView *xing;
@property (nonatomic, retain)UILabel *score;
@property (nonatomic, retain)UILabel *userName;
@property (nonatomic, retain)UIImageView *addressImg;
@property (nonatomic, retain)UILabel *address;
@property (nonatomic, retain)UILabel *short_description;
@property (nonatomic, retain)UILabel *focusAndFan;
@property (nonatomic, retain)UIView *img_View;/**<用来盛放下列img的视图*/
@property (nonatomic, retain)UIImageView *album;/**<专辑*/
@property (nonatomic, retain)UIImageView *like_album;/**<喜欢专辑图片*/
@property (nonatomic, retain)UIImageView *contents;/**<内容*/
@property (nonatomic, retain)UIImageView *clubImg;/**<俱乐部*/
@property (nonatomic, retain)UILabel *line1;/**<分割线1*/
@property (nonatomic, retain)UILabel *line2;/**<分割线2*/
@property (nonatomic, retain)UILabel *line3;/**<分割线3*/
@property (nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, assign)CGRect originalFrame;
@property (nonatomic, retain)UIImageView *lastImageView;

@end

@implementation WMEUserBack
- (void)dealloc
{
    [_head release];
    [_backImg release];
    [_userImg release];
    [_xing release];
    [_score release];
    [_userName release];
    [_addressImg release];
    [_address release];
    [_short_description release];
    [_focusAndFan release];
    [_img_View release];
    [_album release];
    [_like_album release];
    [_contents release];
    [_clubImg release];
    [_line1 release];
    [_line2 release];
    [_line3 release];
    [_dic release];
    [super dealloc];
}


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.userInteractionEnabled = YES;
        //背景图片
        self.backImg = [[UIImageView alloc] init];
        self.contentMode = UIViewContentModeScaleAspectFill;
        self.clipsToBounds = YES;
        self.backImg.userInteractionEnabled = YES;
        [self addSubview:_backImg];
        [_backImg release];
        
        //背景 上的阴影
        self.head = [[UIView alloc] init];
        _head.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
//        _head.backgroundColor = [UIColor purpleColor];
        [self.backImg addSubview:_head];
        [_head release];
        
        //用户头像
        self.userImg = [[UIImageView alloc] init];
//        _userImg.backgroundColor = [UIColor greenColor];
        _userImg.contentMode = UIViewContentModeScaleAspectFill;
        _userImg.layer.cornerRadius = 50;
        _userImg.clipsToBounds = YES;
        [self.backImg addSubview:_userImg];
        _userImg.userInteractionEnabled = YES;
        UITapGestureRecognizer *userImgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showZoomImageView:)];
        [self.userImg addGestureRecognizer:userImgTap];
        [_userImg release];
        [userImgTap release];
        
        
        //星级图标
        self.xing = [[UIImageView alloc] init];
//        _xing.backgroundColor = [UIColor blueColor];
        [self.backImg addSubview:_xing];
        [_xing release];
        
        //星级个数
        self.score = [[UILabel alloc] init];
//        _score.backgroundColor = [UIColor brownColor];
        _score.textAlignment = NSTextAlignmentCenter;
        _score.textColor = [UIColor whiteColor];
        _score.font = [UIFont systemFontOfSize:10];
        [self.backImg addSubview:_score];
        [_score release];
        
        //用户名
        self.userName = [[UILabel alloc] init];
//        _userName.backgroundColor = [UIColor redColor];
        _userName.textAlignment = NSTextAlignmentCenter;
        _userName.textColor = [UIColor whiteColor];
        _userName.font = [UIFont boldSystemFontOfSize:15];
        [self.backImg addSubview:_userName];
        [_userName release];
        
        //地址图标
        self.addressImg = [[UIImageView alloc] init];
        _addressImg.backgroundColor = [UIColor lightGrayColor];
        [self.backImg addSubview:_addressImg];
        [_addressImg release];
        
        //地址
        self.address = [[UILabel alloc] init];
        _address.font = [UIFont systemFontOfSize:10];
        _address.textColor = [UIColor whiteColor];
        _address.textAlignment = NSTextAlignmentCenter;
        [self.backImg addSubview:_address];
        [_address release];
        
        //简介
        self.short_description = [[UILabel alloc] init];
//        _short_description.backgroundColor = [UIColor darkGrayColor];
        _short_description.font = [UIFont systemFontOfSize:10];
        _short_description.textColor = [UIColor whiteColor];
        _short_description.textAlignment = NSTextAlignmentCenter;
        [self.backImg addSubview:_short_description];
        [_short_description release];
        
        //关注 和粉丝
        self.focusAndFan = [[UILabel alloc] init];
//        _focusAndFan.backgroundColor = [UIColor lightGrayColor];
        _focusAndFan.font = [UIFont systemFontOfSize:10];
        _focusAndFan.textColor = [UIColor whiteColor];
        _focusAndFan.textAlignment = NSTextAlignmentCenter;
        [self.backImg addSubview:_focusAndFan];
        [_focusAndFan release];
        
        self.img_View = [[UIView alloc] init];
        _img_View.backgroundColor = [UIColor whiteColor];
        [self.backImg addSubview:_img_View];
        [_img_View release];
        
        //内容img
        self.contents = [[UIImageView alloc] init];
//        _contents.backgroundColor = [UIColor redColor];
        _contents.image = [UIImage imageNamed:@"content"];
        [self.img_View addSubview:_contents];
        [_contents release];
        
        //专辑
        self.album = [[UIImageView alloc] init];
//        _album.backgroundColor = [UIColor yellowColor];
        _album.image = [UIImage imageNamed:@"album"];
        
        [self.img_View addSubview:_album];
        [_album release];
        
        //喜欢专辑
        self.like_album = [[UIImageView alloc] init];
//        _like_album.backgroundColor = [UIColor blueColor];
        _like_album.image = [UIImage imageNamed:@"like"];
        
        [self.img_View addSubview:_like_album];
        [_like_album release];
        
        //club
        self.clubImg = [[UIImageView alloc] init];
//        _clubImg.backgroundColor = [UIColor grayColor];
        _clubImg.image = [UIImage imageNamed:@"club"];
       
        [self.img_View addSubview:_clubImg];
        [_clubImg release];
        
        //分割线1
        self.line1 = [[UILabel alloc] init];
        _line1.backgroundColor = [UIColor lightGrayColor];
        [self.img_View addSubview:_line1];
        [_line1 release];
        
        //分割线2
        self.line2 = [[UILabel alloc] init];
        _line2.backgroundColor = [UIColor lightGrayColor];
        [self.img_View addSubview:_line2];
        [_line2 release];
        
        //分割线3
        self.line3 = [[UILabel alloc] init];
        _line3.backgroundColor = [UIColor lightGrayColor];
        [self.img_View addSubview:_line3];
        [_line3 release];
    }
    return self;
}

- (void)setDic:(NSDictionary *)dic
{
    if (_dic != dic) {
        [_dic release];
        _dic = [dic retain];
       
        for (NSString *str in dic) {
            NSLog(@"%@ ----  %@", str, dic[str]);
        }
        
        [_backImg sd_setImageWithURL:[NSURL URLWithString:_dic[@"background_img_url"]]];
        
        [_userImg sd_setImageWithURL:[NSURL URLWithString:_dic[@"avatar"]]];
        
        _userName.text = _dic[@"username"];
        
        _score.text = [_dic[@"score"] stringValue];
    
        _address.text = _dic[@"city"];
        
        _short_description.text = [NSString stringWithFormat:@"简介:%@", _dic[@"short_description"]];
        
        _focusAndFan.text = [NSString stringWithFormat:@"关注:%@   粉丝%@", _dic[@"follow_count"], _dic[@"be_follow_count"]];
        
    }
}


- (void)layoutSubviews
{
    //背景图片
    _backImg.frame = self.frame;
    
    __block __typeof(&*self)ws = self;
    [_backImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.and.right.equalTo(ws).with.offset(0);
    }];
    
    //背景图上的阴影
//    _head.frame = _backImg.frame;
    [_head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.and.right.equalTo(ws).with.offset(0);
    }];
    //用户头像
//    _userImg.frame = CGRectMake(self.window.center.x - 50, 80, 100, 100);
    [_userImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 100));
        make.centerX.equalTo(ws.mas_centerX);
        make.centerY.equalTo(ws.mas_centerY).with.offset( - 20);
    }];
    //用户名
//    _userName.frame = CGRectMake(50, 100, self.bounds.origin.x - 100, 30);
    [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 25));
        make.left.equalTo(ws.mas_left).with.offset(20);
        make.top.equalTo(_userImg.mas_bottom).with.offset(10);
        make.right.equalTo(ws.mas_right).with.offset(-20);
    }];
    
    //星级
//    _xing = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.center.x - 60, 130 + 10, 10, 10)];
//    [_xing mas_makeConstraints:^(MASConstraintMaker *make) {
    
//    }]
    //星级个数
//    _score.frame = CGRectMake(self.window.center.x - 50, 130 + 10, 100, 10);
    [_score mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 20));
        make.top.equalTo(_userName.mas_bottom);
        make.left.equalTo(ws.mas_left).with.offset(20);
        make.right.equalTo(ws.mas_right).with.offset(-20);
    }];
    if (_address.text.length != 0) {
        //地址
//        _address.frame = CGRectMake(self.window.center.x - 50, 130 + 10 + 10, 100, 10);
        [_address mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(100, 20));
            make.top.equalTo(_score.mas_bottom);
            make.left.equalTo(ws.mas_left).with.offset(20);
            make.right.equalTo(ws.mas_right).with.offset(-20);
        }];
        
    }else{
        [_address mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(0, 0));
            make.top.equalTo(_score.mas_bottom);
            make.left.equalTo(ws.mas_left).with.offset(20);
            make.right.equalTo(ws.mas_right).with.offset(-20);

        }];
    }
    //地址图标
//    _addressImg.frame = CGRectMake(self.window.center.x - 60, 150, 10, 10);
    //简介
//    _short_description.frame =CGRectMake(self.window.center.x - 100, 150 + 10, 200, 35);
    if ([_dic[@"short_description"] length] != 0) {
        [_short_description mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(100, 20));
            make.top.equalTo(_address.mas_bottom);
            make.left.equalTo(ws.mas_left).with.offset(20);
            make.right.equalTo(ws.mas_right).with.offset(-20);
        }];
    }else{
        [_short_description mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(0, 0));
            make.top.equalTo(_address.mas_bottom);
            make.left.equalTo(ws.mas_left).with.offset(20);
            make.right.equalTo (ws.mas_right).with.offset(-20);
        }];
    }
    //关注 和 粉丝
    [_focusAndFan mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(200, 20));
        make.top.equalTo(_short_description.mas_bottom);
        make.left.equalTo(ws.mas_left).with.offset(20);
        make.right.equalTo(ws.mas_right).with.offset(-20);
    }];
    
    [_img_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 50));
        make.left.equalTo(ws.mas_left).with.offset(0);
        make.right.equalTo(ws.mas_right).with.offset(0);
        make.bottom.equalTo(ws.mas_bottom).with.offset(0);
    }];
    
    [_contents mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(ws.mas_bottom).with.offset( -15);
        make.centerY.equalTo(@[_line1, _album, _line2, _like_album, _line3, _clubImg]);
        make.size.mas_equalTo(CGSizeMake(SIZE, SIZE));
    }];
    
    [_line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(0.5, 30));
    }];
    
    [_album mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SIZE, SIZE));
    }];
    _album.userInteractionEnabled = YES;
    UITapGestureRecognizer *albumTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(albumTapAction:)];
    [_album addGestureRecognizer:albumTap];
    
    [_line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(0.5, 30));
    }];
    
    [_like_album mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SIZE, SIZE));
    }];
    _like_album.userInteractionEnabled = YES;
    UITapGestureRecognizer *like_albumTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(like_albumTapAction:)];
    [_like_album addGestureRecognizer:like_albumTap];
    
    [_line3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(0.5, 30));
    }];
    
    [_clubImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(SIZE, SIZE));
    }];
    _clubImg.userInteractionEnabled = YES;
    UITapGestureRecognizer *clubImgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clubImgTapAction:)];
    [_clubImg addGestureRecognizer:clubImgTap];
    [_backImg distributeSpacingHorizontallyWith:@[_contents, _line1, _album, _line2, _like_album, _line3, _clubImg]];
    [albumTap release];
    [like_albumTap release];
    [clubImgTap release];
    NSLog(@"hhhhh");
    
}

#pragma mark - 点击专辑图片
- (void)albumTapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"heee");
    NSString *temp = @"http://www.duitang.com/napi/album/list/by_user/?__dtac=%257B%2522_r%2522%253A%2520%2522178936%2522%257D&app_code=gandalf&app_version=5.9%20rv%3A150681&device_name=Unknown%20iPhone&device_platform=iPhone7%2C2&like=0&limit=0&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=667&screen_width=375&start=0&user_id=10347650";
    NSString *str = [temp stringByReplacingOccurrencesOfString:@"id=10347650" withString:[NSString stringWithFormat:@"id=%@",_dic[@"id"]]];
    [self.delegate pushAction:str];
}

#pragma mark - 点击喜欢专辑图片
- (void)like_albumTapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"like");
    NSString *temp = @"http://www.duitang.com/napi/album/list/by_user/?__dtac=%257B%2522_r%2522%253A%2520%2522729075%2522%257D&app_code=gandalf&app_version=5.9%20rv%3A150681&device_name=Unknown%20iPhone&device_platform=iPhone7%2C2&like=1&limit=0&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=667&screen_width=375&start=0&user_id=10347650";
    NSString *str = [temp stringByReplacingOccurrencesOfString:@"id=10347650" withString:[NSString stringWithFormat:@"id=%@",_dic[@"id"]]];
    [self.delegate pushAction:str];
}

#pragma mark - 点击俱乐部图片
- (void)clubImgTapAction:(UITapGestureRecognizer *)tap
{
    NSLog(@"club");
    NSString *temp = @"http://www.duitang.com/napi/club/list/by_user_id/?__dtac=%257B%2522_r%2522%253A%2520%2522714581%2522%257D&app_code=gandalf&app_version=5.9%20rv%3A150681&device_name=Unknown%20iPhone&device_platform=iPhone7%2C2&include_fields=check_in&limit=0&locale=zh_CN&platform_name=iPhone%20OS&platform_version=9.2&screen_height=667&screen_width=375&start=0&user_id=10347650";
    NSString *str = [temp stringByReplacingOccurrencesOfString:@"user_id=10347650" withString:[NSString stringWithFormat:@"user_id=%@", _dic[@"id"]]];
    [self.clubDelegate pushClubAction:str];
}
#pragma mark - 点击自己的头像
-(void)showZoomImageView:(UITapGestureRecognizer *)tap
{
    if (![(UIImageView *)tap.view image]) {
        return;
    }
    //scrollView作为背景
    UIScrollView *bgView = [[UIScrollView alloc] init];
    bgView.frame = [UIScreen mainScreen].bounds;
    bgView.backgroundColor = [UIColor blackColor];
    UITapGestureRecognizer *tapBg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBgView:)];
    [bgView addGestureRecognizer:tapBg];
    
    UIImageView *picView = (UIImageView *)tap.view;
    
    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = picView.image;
    imageView.frame = [bgView convertRect:picView.frame fromView:self];
    [bgView addSubview:imageView];
    
    [[[UIApplication sharedApplication] keyWindow] addSubview:bgView];
    
    self.lastImageView = imageView;
    self.originalFrame = imageView.frame;
    self.scrollView = bgView;
    //最大放大比例
    self.scrollView.maximumZoomScale = 1.5;
    self.scrollView.delegate = self;
    
    [UIView animateWithDuration:0.5 animations:^{
        CGRect frame = imageView.frame;
        frame.size.width = bgView.frame.size.width;
        frame.size.height = frame.size.width * (imageView.image.size.height / imageView.image.size.width);
        frame.origin.x = 0;
        frame.origin.y = (bgView.frame.size.height - frame.size.height) * 0.5;
        imageView.frame = frame;
    }];
}

-(void)tapBgView:(UITapGestureRecognizer *)tapBgRecognizer
{
    self.scrollView.contentOffset = CGPointZero;
    [UIView animateWithDuration:0.5 animations:^{
        self.lastImageView.frame = self.originalFrame;
        tapBgRecognizer.view.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [tapBgRecognizer.view removeFromSuperview];
        self.scrollView = nil;
        self.lastImageView = nil;
    }];
}

//返回可缩放的视图
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return self.lastImageView;
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
